package com.inei.reportedeverificacion.model.dao;

import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.inei.reportedeverificacion.model.connection.UtilDB;
import com.inei.reportedeverificacion.model.entity.Reporte;

public class ReporteDao implements ICRUD<Reporte> {

	// variables
	private UtilDB util;
	private SQLiteDatabase db;
	private Cursor cursor = null;
	private Context context;

	// constructor
	public ReporteDao(Context context) {
		this.context = context;
		util = new UtilDB(context);
	}

	// metodos CRUD
	@Override
	public Reporte buscar(Reporte o) {
		Reporte obj = null;
		db = util.getReadableDatabase();
		cursor = db.rawQuery("select * from Reporte where CLAVEVIVHOGAR=?",
				new String[] { o.getCLAVEVIVHOGAR() + "" });
		if (cursor.moveToFirst()) {
			obj = new Reporte();
			obj.setCLAVEVIVHOGAR(cursor.getString(cursor
					.getColumnIndex("CLAVEVIVHOGAR")));
			obj.setCLAVEVIV(cursor.getString(cursor.getColumnIndex("CLAVEVIV")));
			obj.setCcdd(cursor.getString(cursor.getColumnIndex("ccdd")));
			obj.setCcpp(cursor.getString(cursor.getColumnIndex("ccpp")));
			obj.setCcdi(cursor.getString(cursor.getColumnIndex("ccdi")));
			obj.setIdgrupo(cursor.getString(cursor.getColumnIndex("idgrupo")));
			obj.setHOGAR(cursor.getString(cursor.getColumnIndex("HOGAR")));
			obj.setVIVREM(cursor.getInt(cursor.getColumnIndex("VIVREM")));
			obj.setC1P15_RFINAL(cursor.getInt(cursor
					.getColumnIndex("C1P15_RFINAL")));
			obj.setC1P18(cursor.getInt(cursor.getColumnIndex("C1P18")));
			obj.setT_05_DIG_CAP03_COUNT(cursor.getInt(cursor
					.getColumnIndex("T_05_DIG_CAP03_COUNT")));
			obj.setC1P59(cursor.getInt(cursor.getColumnIndex("C1P59")));
			obj.setC1P67_1(cursor.getInt(cursor.getColumnIndex("C1P67_1")));
			obj.setC1P67_2(cursor.getInt(cursor.getColumnIndex("C1P67_2")));
			obj.setC1P67_3(cursor.getInt(cursor.getColumnIndex("C1P67_3")));
			obj.setC1P67_4(cursor.getInt(cursor.getColumnIndex("C1P67_4")));
			obj.setC1P67_5(cursor.getInt(cursor.getColumnIndex("C1P67_5")));
			obj.setC1P67_6(cursor.getInt(cursor.getColumnIndex("C1P67_6")));
			obj.setC1P67_7(cursor.getInt(cursor.getColumnIndex("C1P67_7")));
			obj.setC1P67_8(cursor.getInt(cursor.getColumnIndex("C1P67_8")));
			obj.setC1P67_9(cursor.getInt(cursor.getColumnIndex("C1P67_9")));
			obj.setC1P67_10(cursor.getInt(cursor.getColumnIndex("C1P67_10")));
			obj.setC1P67_11(cursor.getInt(cursor.getColumnIndex("C1P67_11")));
			obj.setC1P69(cursor.getInt(cursor.getColumnIndex("C1P69")));
			obj.setC1P70(cursor.getInt(cursor.getColumnIndex("C1P70")));
			obj.setC1P78_1(cursor.getInt(cursor.getColumnIndex("C1P78_1")));
			obj.setC1P79_1(cursor.getInt(cursor.getColumnIndex("C1P79_1")));
			obj.setC1P78_2(cursor.getInt(cursor.getColumnIndex("C1P78_2")));
			obj.setC1P79_2(cursor.getInt(cursor.getColumnIndex("C1P79_2")));
			obj.setC1P78_3(cursor.getInt(cursor.getColumnIndex("C1P78_3")));
			obj.setC1P79_3(cursor.getInt(cursor.getColumnIndex("C1P79_3")));
			obj.setC1P78_4(cursor.getInt(cursor.getColumnIndex("C1P78_4")));
			obj.setC1P79_4(cursor.getInt(cursor.getColumnIndex("C1P79_4")));
			obj.setC1P78_5(cursor.getInt(cursor.getColumnIndex("C1P78_5")));
			obj.setC1P79_5(cursor.getInt(cursor.getColumnIndex("C1P79_5")));
			obj.setC1P78_6(cursor.getInt(cursor.getColumnIndex("C1P78_6")));
			obj.setC1P79_6(cursor.getInt(cursor.getColumnIndex("C1P79_6")));
			obj.setC1P78_7(cursor.getInt(cursor.getColumnIndex("C1P78_7")));
			obj.setC1P79_7(cursor.getInt(cursor.getColumnIndex("C1P79_7")));
			obj.setC1P78_8(cursor.getInt(cursor.getColumnIndex("C1P78_8")));
			obj.setC1P79_8(cursor.getInt(cursor.getColumnIndex("C1P79_8")));
			obj.setC1P78_9(cursor.getInt(cursor.getColumnIndex("C1P78_9")));
			obj.setC1P79_9(cursor.getInt(cursor.getColumnIndex("C1P79_9")));
			obj.setC1P78_10(cursor.getInt(cursor.getColumnIndex("C1P78_10")));
			obj.setC1P79_10(cursor.getInt(cursor.getColumnIndex("C1P79_10")));
			obj.setC1P78_11(cursor.getInt(cursor.getColumnIndex("C1P78_11")));
			obj.setC1P79_11(cursor.getInt(cursor.getColumnIndex("C1P79_11")));
			obj.setC1P84(cursor.getInt(cursor.getColumnIndex("C1P84")));
			obj.setC1P85(cursor.getInt(cursor.getColumnIndex("C1P85")));
			obj.setC1P95(cursor.getInt(cursor.getColumnIndex("C1P95")));
			obj.setC1P97(cursor.getInt(cursor.getColumnIndex("C1P97")));
			obj.setC1P97_COUNT(cursor.getInt(cursor
					.getColumnIndex("C1P97_COUNT")));
			obj.setC1P109(cursor.getInt(cursor.getColumnIndex("C1P109")));
			obj.setC1P118(cursor.getInt(cursor.getColumnIndex("C1P118")));
			obj.setC1P132(cursor.getInt(cursor.getColumnIndex("C1P132")));
			obj.setC1P142(cursor.getInt(cursor.getColumnIndex("C1P142")));
			obj.setC1P142_COUNT(cursor.getInt(cursor
					.getColumnIndex("C1P142_COUNT")));
			obj.setC1P340_INF(cursor.getInt(cursor.getColumnIndex("C1P340_INF")));
		}
		Cerrar();
		return obj;
	}

	public List<Reporte> leerXCLAVEVIV(String clave) {
		List<Reporte> lista = null;
		db = util.getReadableDatabase();
		cursor = db.rawQuery("select * from Reporte where CLAVEVIV=?",
				new String[] { clave });
		if (cursor.moveToFirst()) {
			lista = new ArrayList<Reporte>();
			do {
				Reporte obj = new Reporte();
				obj.setCLAVEVIVHOGAR(cursor.getString(cursor
						.getColumnIndex("CLAVEVIVHOGAR")));
				obj.setCLAVEVIV(cursor.getString(cursor
						.getColumnIndex("CLAVEVIV")));
				obj.setCcdd(cursor.getString(cursor.getColumnIndex("ccdd")));
				obj.setCcpp(cursor.getString(cursor.getColumnIndex("ccpp")));
				obj.setCcdi(cursor.getString(cursor.getColumnIndex("ccdi")));
				obj.setIdgrupo(cursor.getString(cursor
						.getColumnIndex("idgrupo")));
				obj.setHOGAR(cursor.getString(cursor.getColumnIndex("HOGAR")));
				obj.setVIVREM(cursor.getInt(cursor.getColumnIndex("VIVREM")));
				obj.setC1P15_RFINAL(cursor.getInt(cursor
						.getColumnIndex("C1P15_RFINAL")));
				obj.setC1P18(cursor.getInt(cursor.getColumnIndex("C1P18")));
				obj.setT_05_DIG_CAP03_COUNT(cursor.getInt(cursor
						.getColumnIndex("T_05_DIG_CAP03_COUNT")));
				obj.setC1P59(cursor.getInt(cursor.getColumnIndex("C1P59")));
				obj.setC1P67_1(cursor.getInt(cursor.getColumnIndex("C1P67_1")));
				obj.setC1P67_2(cursor.getInt(cursor.getColumnIndex("C1P67_2")));
				obj.setC1P67_3(cursor.getInt(cursor.getColumnIndex("C1P67_3")));
				obj.setC1P67_4(cursor.getInt(cursor.getColumnIndex("C1P67_4")));
				obj.setC1P67_5(cursor.getInt(cursor.getColumnIndex("C1P67_5")));
				obj.setC1P67_6(cursor.getInt(cursor.getColumnIndex("C1P67_6")));
				obj.setC1P67_7(cursor.getInt(cursor.getColumnIndex("C1P67_7")));
				obj.setC1P67_8(cursor.getInt(cursor.getColumnIndex("C1P67_8")));
				obj.setC1P67_9(cursor.getInt(cursor.getColumnIndex("C1P67_9")));
				obj.setC1P67_10(cursor.getInt(cursor.getColumnIndex("C1P67_10")));
				obj.setC1P67_11(cursor.getInt(cursor.getColumnIndex("C1P67_11")));
				obj.setC1P69(cursor.getInt(cursor.getColumnIndex("C1P69")));
				obj.setC1P70(cursor.getInt(cursor.getColumnIndex("C1P70")));
				obj.setC1P78_1(cursor.getInt(cursor.getColumnIndex("C1P78_1")));
				obj.setC1P79_1(cursor.getInt(cursor.getColumnIndex("C1P79_1")));
				obj.setC1P78_2(cursor.getInt(cursor.getColumnIndex("C1P78_2")));
				obj.setC1P79_2(cursor.getInt(cursor.getColumnIndex("C1P79_2")));
				obj.setC1P78_3(cursor.getInt(cursor.getColumnIndex("C1P78_3")));
				obj.setC1P79_3(cursor.getInt(cursor.getColumnIndex("C1P79_3")));
				obj.setC1P78_4(cursor.getInt(cursor.getColumnIndex("C1P78_4")));
				obj.setC1P79_4(cursor.getInt(cursor.getColumnIndex("C1P79_4")));
				obj.setC1P78_5(cursor.getInt(cursor.getColumnIndex("C1P78_5")));
				obj.setC1P79_5(cursor.getInt(cursor.getColumnIndex("C1P79_5")));
				obj.setC1P78_6(cursor.getInt(cursor.getColumnIndex("C1P78_6")));
				obj.setC1P79_6(cursor.getInt(cursor.getColumnIndex("C1P79_6")));
				obj.setC1P78_7(cursor.getInt(cursor.getColumnIndex("C1P78_7")));
				obj.setC1P79_7(cursor.getInt(cursor.getColumnIndex("C1P79_7")));
				obj.setC1P78_8(cursor.getInt(cursor.getColumnIndex("C1P78_8")));
				obj.setC1P79_8(cursor.getInt(cursor.getColumnIndex("C1P79_8")));
				obj.setC1P78_9(cursor.getInt(cursor.getColumnIndex("C1P78_9")));
				obj.setC1P79_9(cursor.getInt(cursor.getColumnIndex("C1P79_9")));
				obj.setC1P78_10(cursor.getInt(cursor.getColumnIndex("C1P78_10")));
				obj.setC1P79_10(cursor.getInt(cursor.getColumnIndex("C1P79_10")));
				obj.setC1P78_11(cursor.getInt(cursor.getColumnIndex("C1P78_11")));
				obj.setC1P79_11(cursor.getInt(cursor.getColumnIndex("C1P79_11")));
				obj.setC1P84(cursor.getInt(cursor.getColumnIndex("C1P84")));
				obj.setC1P85(cursor.getInt(cursor.getColumnIndex("C1P85")));
				obj.setC1P95(cursor.getInt(cursor.getColumnIndex("C1P95")));
				obj.setC1P97(cursor.getInt(cursor.getColumnIndex("C1P97")));
				obj.setC1P97_COUNT(cursor.getInt(cursor
						.getColumnIndex("C1P97_COUNT")));
				obj.setC1P109(cursor.getInt(cursor.getColumnIndex("C1P109")));
				obj.setC1P118(cursor.getInt(cursor.getColumnIndex("C1P118")));
				obj.setC1P132(cursor.getInt(cursor.getColumnIndex("C1P132")));
				obj.setC1P142(cursor.getInt(cursor.getColumnIndex("C1P142")));
				obj.setC1P142_COUNT(cursor.getInt(cursor
						.getColumnIndex("C1P142_COUNT")));
				obj.setC1P340_INF(cursor.getInt(cursor
						.getColumnIndex("C1P340_INF")));
				lista.add(obj);
			} while (cursor.moveToNext());
		}
		Cerrar();
		return lista;
	}

	@Override
	public int crear(Reporte o) {
		int resultado = 0;
		db = util.getWritableDatabase();
		db.beginTransaction();
		ContentValues valores = new ContentValues();
		valores.put("CLAVEVIVHOGAR", o.getCLAVEVIVHOGAR());
		valores.put("CLAVEVIV", o.getCLAVEVIV());
		valores.put("ccdd", o.getCcdd());
		valores.put("ccpp", o.getCcpp());
		valores.put("ccdi", o.getCcdi());
		valores.put("idgrupo", o.getIdgrupo());
		valores.put("HOGAR", o.getHOGAR());
		valores.put("VIVREM", o.getVIVREM());
		valores.put("C1P15_RFINAL", o.getC1P15_RFINAL());
		valores.put("C1P18", o.getC1P18());
		valores.put("T_05_DIG_CAP03_COUNT", o.getT_05_DIG_CAP03_COUNT());
		valores.put("C1P59", o.getC1P59());
		valores.put("C1P67_1", o.getC1P67_1());
		valores.put("C1P67_2", o.getC1P67_2());
		valores.put("C1P67_3", o.getC1P67_3());
		valores.put("C1P67_4", o.getC1P67_4());
		valores.put("C1P67_5", o.getC1P67_5());
		valores.put("C1P67_6", o.getC1P67_6());
		valores.put("C1P67_7", o.getC1P67_7());
		valores.put("C1P67_8", o.getC1P67_8());
		valores.put("C1P67_9", o.getC1P67_9());
		valores.put("C1P67_10", o.getC1P67_10());
		valores.put("C1P67_11", o.getC1P67_11());
		valores.put("C1P69", o.getC1P69());
		valores.put("C1P70", o.getC1P70());
		valores.put("C1P78_1", o.getC1P78_1());
		valores.put("C1P79_1", o.getC1P79_1());
		valores.put("C1P78_2", o.getC1P78_2());
		valores.put("C1P79_2", o.getC1P79_2());
		valores.put("C1P78_3", o.getC1P78_3());
		valores.put("C1P79_3", o.getC1P79_3());
		valores.put("C1P78_4", o.getC1P78_4());
		valores.put("C1P79_4", o.getC1P79_4());
		valores.put("C1P78_5", o.getC1P78_5());
		valores.put("C1P79_5", o.getC1P79_5());
		valores.put("C1P78_6", o.getC1P78_6());
		valores.put("C1P79_6", o.getC1P79_6());
		valores.put("C1P78_7", o.getC1P78_7());
		valores.put("C1P79_7", o.getC1P79_7());
		valores.put("C1P78_8", o.getC1P78_8());
		valores.put("C1P79_8", o.getC1P79_8());
		valores.put("C1P78_9", o.getC1P78_9());
		valores.put("C1P79_9", o.getC1P79_9());
		valores.put("C1P78_10", o.getC1P78_10());
		valores.put("C1P79_10", o.getC1P79_10());
		valores.put("C1P78_11", o.getC1P78_11());
		valores.put("C1P79_11", o.getC1P79_11());
		valores.put("C1P84", o.getC1P84());
		valores.put("C1P85", o.getC1P85());
		valores.put("C1P95", o.getC1P95());
		valores.put("C1P97", o.getC1P97());
		valores.put("C1P97_COUNT", o.getC1P97_COUNT());
		valores.put("C1P109", o.getC1P109());
		valores.put("C1P118", o.getC1P118());
		valores.put("C1P132", o.getC1P132());
		valores.put("C1P142", o.getC1P142());
		valores.put("C1P142_COUNT", o.getC1P142_COUNT());
		valores.put("C1P340_INF", o.getC1P340_INF());

		resultado = (int) db.insert("Reporte", null, valores);
		if (resultado > 0) {
			Log.i("ReporteDao", "Exito");
			db.setTransactionSuccessful();
		}
		valores.clear();
		Cerrar();
		return resultado;
	}

	@Override
	public int eliminar(Reporte o) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int actualizar(Reporte o) {
		int resultado = 0;
		db = util.getWritableDatabase();
		db.beginTransaction();
		ContentValues valores = new ContentValues();
		valores.put("VIVREM", o.getVIVREM());
		valores.put("C1P15_RFINAL", o.getC1P15_RFINAL());
		valores.put("C1P18", o.getC1P18());
		valores.put("T_05_DIG_CAP03_COUNT", o.getT_05_DIG_CAP03_COUNT());
		valores.put("C1P59", o.getC1P59());
		valores.put("C1P67_1", o.getC1P67_1());
		valores.put("C1P67_2", o.getC1P67_2());
		valores.put("C1P67_3", o.getC1P67_3());
		valores.put("C1P67_4", o.getC1P67_4());
		valores.put("C1P67_5", o.getC1P67_5());
		valores.put("C1P67_6", o.getC1P67_6());
		valores.put("C1P67_7", o.getC1P67_7());
		valores.put("C1P67_8", o.getC1P67_8());
		valores.put("C1P67_9", o.getC1P67_9());
		valores.put("C1P67_10", o.getC1P67_10());
		valores.put("C1P67_11", o.getC1P67_11());
		valores.put("C1P69", o.getC1P69());
		valores.put("C1P70", o.getC1P70());
		valores.put("C1P78_1", o.getC1P78_1());
		valores.put("C1P79_1", o.getC1P79_1());
		valores.put("C1P78_2", o.getC1P78_2());
		valores.put("C1P79_2", o.getC1P79_2());
		valores.put("C1P78_3", o.getC1P78_3());
		valores.put("C1P79_3", o.getC1P79_3());
		valores.put("C1P78_4", o.getC1P78_4());
		valores.put("C1P79_4", o.getC1P79_4());
		valores.put("C1P78_5", o.getC1P78_5());
		valores.put("C1P79_5", o.getC1P79_5());
		valores.put("C1P78_6", o.getC1P78_6());
		valores.put("C1P79_6", o.getC1P79_6());
		valores.put("C1P78_7", o.getC1P78_7());
		valores.put("C1P79_7", o.getC1P79_7());
		valores.put("C1P78_8", o.getC1P78_8());
		valores.put("C1P79_8", o.getC1P79_8());
		valores.put("C1P78_9", o.getC1P78_9());
		valores.put("C1P79_9", o.getC1P79_9());
		valores.put("C1P78_10", o.getC1P78_10());
		valores.put("C1P79_10", o.getC1P79_10());
		valores.put("C1P78_11", o.getC1P78_11());
		valores.put("C1P79_11", o.getC1P79_11());
		valores.put("C1P84", o.getC1P84());
		valores.put("C1P85", o.getC1P85());
		valores.put("C1P95", o.getC1P95());
		valores.put("C1P97", o.getC1P97());
		valores.put("C1P97_COUNT", o.getC1P97_COUNT());
		valores.put("C1P109", o.getC1P109());
		valores.put("C1P118", o.getC1P118());
		valores.put("C1P132", o.getC1P132());
		valores.put("C1P142", o.getC1P142());
		valores.put("C1P142_COUNT", o.getC1P142_COUNT());
		valores.put("C1P340_INF", o.getC1P340_INF());

		resultado = (int) db.update("Reporte", valores, "CLAVEVIVHOGAR=?",
				new String[] { o.getCLAVEVIVHOGAR() + "" });
		if (resultado > 0) {
			db.setTransactionSuccessful();
		}
		valores.clear();
		Cerrar();
		return resultado;
	}

	private void Cerrar() {
		if (cursor != null) {
			cursor.close();
		}
		if (db.inTransaction()) {
			db.endTransaction();
		}
		db.close();
	}

	@Override
	public List<Reporte> leer() {
		// TODO Auto-generated method stub
		return null;
	}
}
